/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.util.state.InventoryState;
import Merlion.util.state.InventoryState;

import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name = "ProductManagedBean")
@ViewScoped
public class ProductManagedBean {

    private InventoryState newProduct = new InventoryState();
    private InventoryState selectedProduct;
    private InventoryState[] selectedProducts;
    private InventoryState selectedViewProduct;
    private List<InventoryState> productList;
    private boolean editMode;
    @EJB
    private InventoryItemsBeanRemote iib;

    /** Creates a new instance of ProductManagedBean */
    public ProductManagedBean() {
    }

    public void addProduct(ActionEvent event) {
        System.out.println("Add product");
        String name = newProduct.getName();
        String desc = newProduct.getDescription();
        int shelfLife = newProduct.getShelfLife();
        double caseWeight = newProduct.getCaseWeight();
        double basePrice = newProduct.getBasePrice();
        int productionRate = (int) newProduct.getProductionRate();
        double listPrice = newProduct.getListPrice();
        double limitPrice = newProduct.getLimitPrice();
   
        iib.createProduct2(name, desc, shelfLife, caseWeight, basePrice, productionRate, listPrice, limitPrice);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("products.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteProduct(ActionEvent event) {
        if (selectedProduct != null) {
            System.out.println("Selected product not null");
            Long id = selectedProduct.getItemId();
            iib.deleteProduct(id);
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("products.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteProducts() {
        System.out.println("Delete products");

        if (selectedProducts != null) {
            System.out.println(":::WEB::: If selected products not null");
            for (int i = 0; i < selectedProducts.length; i++) {
                System.out.println("selectedProducts i: " + i + " - " + selectedProducts[i].getItemId());
                iib.deleteProduct(selectedProducts[i].getItemId());
            }
        } else {
            System.out.println(":::WEB::: Selected products are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("products.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void editProduct(ActionEvent event) {
        if (selectedViewProduct != null) {
            Long id = selectedViewProduct.getItemId();
            String name = selectedViewProduct.getName();
            String desc = selectedViewProduct.getDescription();
            int shelfLife = selectedViewProduct.getShelfLife();
            double caseWeight = selectedViewProduct.getCaseWeight();
            double basePrice = selectedViewProduct.getBasePrice();
            int productionRate = (int) selectedViewProduct.getProductionRate();
            double listPrice = selectedViewProduct.getListPrice();
            double limitPrice = selectedViewProduct.getLimitPrice();
         //   int inHandQty = selectedViewProduct.getInHandQty();
         //   int reservedQty = selectedViewProduct.getReservedQty();

            iib.updateProduct(id, name, desc,
                    shelfLife, caseWeight, basePrice, limitPrice, listPrice, productionRate);
        }


        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("products.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void onRowEditProduct(RowEditEvent event) {

        System.out.println("On Row Edit product");
        InventoryState eProduct = (InventoryState) event.getObject();

        Long id = eProduct.getItemId();
        String name = eProduct.getName();
        String desc = eProduct.getDescription();
        int shelfLife = eProduct.getShelfLife();
        double caseWeight = eProduct.getCaseWeight();
        double basePrice = eProduct.getBasePrice();
        int productionRate = (int) eProduct.getProductionRate();
        double listPrice = eProduct.getListPrice();
        double limitPrice = eProduct.getLimitPrice();
      //  int inHandQty = eProduct.getInHandQty();
      //  int reservedQty = eProduct.getReservedQty();

        iib.updateProduct(id, name, desc, 
                shelfLife, caseWeight, basePrice, limitPrice, listPrice, productionRate);
    }

    public InventoryState getNewProduct() {
        return newProduct;
    }

    public void setNewProduct(InventoryState newProduct) {
        this.newProduct = newProduct;
    }

    public List<InventoryState> getProductList() {
        if (productList == null) {
            productList = iib.viewProducts();
        }
        return productList;
    }

    public void setProductList(List<InventoryState> productList) {
        this.productList = productList;
    }

    public InventoryState getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(InventoryState selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public InventoryState[] getSelectedProducts() {
        return selectedProducts;
    }

    public void setSelectedProducts(InventoryState[] selectedProducts) {
        this.selectedProducts = selectedProducts;
    }

    public InventoryState getSelectedViewProduct() {
        return selectedViewProduct;
    }

    public void setSelectedViewProduct(InventoryState selectedViewProduct) {
        this.selectedViewProduct = selectedViewProduct;
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (productList == null) {
            productList = iib.viewProducts();
        }
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }
}
